package com.dianping.cat.home.alert.summary.transform;

import com.dianping.cat.home.alert.summary.entity.Alert;
import com.dianping.cat.home.alert.summary.entity.AlertSummary;
import com.dianping.cat.home.alert.summary.entity.Category;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;







public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public Alert buildAlert(Attributes attributes) {
/*  22 */     String alertTime = attributes.getValue("alertTime");
/*  23 */     String type = attributes.getValue("type");
    String metric = attributes.getValue("metric");
    String context = attributes.getValue("context");
/*  26 */     String domain = attributes.getValue("domain");
/*  27 */     String count = attributes.getValue("count");
/*  28 */     Alert alert = new Alert();

/*  30 */     if (alertTime != null) {
      alert.setAlertTime(toDate(alertTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  34 */     if (type != null) {
/*  35 */       alert.setType(type);
    }

/*  38 */     if (metric != null) {
/*  39 */       alert.setMetric(metric);
    }

/*  42 */     if (context != null) {
/*  43 */       alert.setContext(context);
    }

/*  46 */     if (domain != null) {
/*  47 */       alert.setDomain(domain);
    }

/*  50 */     if (count != null) {
/*  51 */       alert.setCount((Integer)convert(Integer.class, count, null));
    }

/*  54 */     return alert;
  }


  public AlertSummary buildAlertSummary(Attributes attributes) {
/*  59 */     String alertDate = attributes.getValue("alert-date");
/*  60 */     String domain = attributes.getValue("domain");
/*  61 */     AlertSummary alertSummary = new AlertSummary();

/*  63 */     if (alertDate != null) {
/*  64 */       alertSummary.setAlertDate(toDate(alertDate, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  67 */     if (domain != null) {
/*  68 */       alertSummary.setDomain(domain);
    }

/*  71 */     return alertSummary;
  }


  public Category buildCategory(Attributes attributes) {
/*  76 */     String name = attributes.getValue("name");
/*  77 */     return new Category(name);
  }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  84 */     if (value == null || value.length() == 0) {
/*  85 */       return defaultValue;
    }

/*  88 */     if (type == Boolean.class || type == boolean.class)
/*  89 */       return (T)Boolean.valueOf(value); 
/*  90 */     if (type == Integer.class || type == int.class)
/*  91 */       return (T)Integer.valueOf(value); 
/*  92 */     if (type == Long.class || type == long.class)
/*  93 */       return (T)Long.valueOf(value); 
/*  94 */     if (type == Short.class || type == short.class)
      return (T)Short.valueOf(value);
/*  96 */     if (type == Float.class || type == float.class)
/*  97 */       return (T)Float.valueOf(value); 
/*  98 */     if (type == Double.class || type == double.class)
/*  99 */       return (T)Double.valueOf(value); 
/* 100 */     if (type == Byte.class || type == byte.class)
/* 101 */       return (T)Byte.valueOf(value); 
/* 102 */     if (type == Character.class || type == char.class) {
/* 103 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 105 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 110 */     if (str == null || str.length() == 0) {
/* 111 */       return defaultValue;
    }

    try {
/* 115 */       return (new SimpleDateFormat(format)).parse(str);
/* 116 */     } catch (ParseException e) {
/* 117 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/alert/summary/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */